package Constructions;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyListener;
import java.util.ArrayList;

import FrameWork.Entity;
import Main.SpaceGame;
import World.Tile;

public abstract class Construction extends Entity implements KeyListener
{
	boolean flashing = false;
	protected boolean powered = false;
	public int width = 1;
	public int height = 1;
	int flashCnt = 20;
	public int textColorIndex = 1;
	protected SpaceGame main;
	public Tile workingTile;
	
	public ArrayList<String> text = new ArrayList<String>();
	public ArrayList<Color> colors = new ArrayList<Color>();
	
	public Construction(int x, int y,SpaceGame main) 
	{
		super(x, y);
		this.main = main;
		workingTile = main.world.tiles[x][y];
	}

	@Override
	public abstract void draw(Graphics g);
	
	public abstract void setText();
	
	public abstract String getName();
	
	public void update()
	{
		checkPowered(x, y);
	}
	
	public int getHeight()
	{
		return height;
	}
	
	public int getWidth()
	{
		return width;
	}
	
	public void setFlashing(boolean f)
	{
		this.flashing = f;
	}
	
	public boolean isFlashing()
	{
		return flashing;
	}
	
	public void drawFlash(Graphics g,int x,int y)
	{
		if (isFlashing() && flashCnt <= 7)
		{
			g.setColor(Color.cyan);
			g.fillRect(x*10, y*10, width*10, height*10);
			if (flashCnt <= 0)
				flashCnt = 20;
		}
		if (isFlashing())
		{
			flashCnt--;
		}
		else 
			flashCnt = 20;
	}
	
	public void checkPowered(int x, int y)
	{
		boolean hasPower = false;
		for(int dx = x;dx<x+width;dx++)
		{
			for(int dy = y;dy<y+height;dy++)
			{
				if (main.world.tiles[dx][dy].isPowered())
					hasPower = true;
			}
		}
		if (hasPower)
			powered = true;
		else 
			powered = false;
	}
}
